/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mosta
 */
public class CustomerDataBase {
    
    private Connection conn;
    private Statement stmt;
    private ResultSet result;
    private PreparedStatement prepStmt;
    
    public CustomerDataBase()
    {
        DatabaseConnection connection = new DatabaseConnection();
        conn = connection.startConnection();
    }
    
    public int fillCustomer(CustomerBean customer)
    {
        int value=0;
        String chkSql="select email from customer where email=? ";
        String sql="insert into customer"
                +"(firstName,lastName,birthdate,email,Mobile,passwd,address,account,c_img)"
                +"VALUES (?,?,?,?,?,?,?,?,?)";
        try {
            prepStmt = conn.prepareStatement(chkSql);
            prepStmt.setString(1,customer.getUserMail());
            result = prepStmt.executeQuery();
            if(result.next())
            {
                return value;
            }
            else
            {
                prepStmt = conn.prepareStatement(sql);
                prepStmt.setString(1, customer.getFirstName());
                prepStmt.setString(2, customer.getLastName());
                prepStmt.setString(3, customer.getUserBDate());
                prepStmt.setString(4, customer.getUserMail());
                prepStmt.setString(5, customer.getUserMobile());
                prepStmt.setString(6, customer.getUserPassword());
                prepStmt.setString(7, customer.getUserAddress());
                prepStmt.setFloat(8, customer.getUserCredit());
                prepStmt.setString(9, customer.getC_img());
                value = prepStmt.executeUpdate();
            
            }
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return value;
    }
    public CustomerBean loginCustomer(String mail,String password){
        CustomerBean bean=new CustomerBean();
        try {
            String chkSql="select * from customer where email=? and passwd=? ";
            
            prepStmt = conn.prepareStatement(chkSql);
            prepStmt.setString(1,mail);
            prepStmt.setString(2,password);
            result = prepStmt.executeQuery();
            if(result.next())
            {
                bean.setC_id(result.getInt("c_id"));
                bean.setFirstName(result.getString("firstName"));
                bean.setLastName(result.getString("lastName"));
                bean.setUserBDate(result.getString("birthdate"));
                bean.setUserMail(result.getString("email"));
                bean.setUserMobile(result.getString("Mobile"));
                bean.setUserPassword(result.getString("passwd"));
                bean.setUserAddress(result.getString("address"));
                bean.setUserCredit(result.getFloat("account"));
                bean.setC_img(result.getString("c_img"));
                
            }
        } catch (SQLException ex) {
            Logger.getLogger(CustomerDataBase.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bean;
    } 
    public CustomerBean updateUserProfile(CustomerBean bean){
        CustomerBean cBean=null;
        int id=bean.getC_id();
        int value=0;
        String sql="Update customer set firstName=? , lastName=? , email=? , passwd=? and birthdate=? , account=? , Mobile=? ,address=? where c_id="+id;
        try {
            prepStmt = conn.prepareStatement(sql);
            prepStmt.setString(1, bean.getFirstName());
            prepStmt.setString(2, bean.getLastName());
            prepStmt.setString(3, bean.getUserMail());
            prepStmt.setString(4, bean.getUserPassword());
            prepStmt.setString(5, bean.getUserBDate());
            prepStmt.setFloat(6, bean.getUserCredit());
            prepStmt.setString(7, bean.getUserMobile());
            prepStmt.setString(8, bean.getUserAddress());
            value = prepStmt.executeUpdate();
            if(value>=1){
              cBean=getUserProfile(id); 
            }
        } catch (SQLException ex) {
            Logger.getLogger(CustomerDataBase.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        return cBean;
    }
    public CustomerBean getUserProfile(int id){
         CustomerBean bean=new CustomerBean();
        try {
            String chkSql="select * from customer where c_id=?";
            
            prepStmt = conn.prepareStatement(chkSql);
            prepStmt.setInt(1,id);
            
            result = prepStmt.executeQuery();
            if(result.next())
            {
                bean.setC_id(result.getInt("c_id"));
                bean.setFirstName(result.getString("firstName"));
                bean.setLastName(result.getString("lastName"));
                bean.setUserBDate(result.getString("birthdate"));
                bean.setUserMail(result.getString("email"));
                bean.setUserMobile(result.getString("Mobile"));
                bean.setUserPassword(result.getString("passwd"));
                bean.setUserAddress(result.getString("address"));
                bean.setUserCredit(result.getFloat("account"));
                bean.setC_img(result.getString("c_img"));
                
            }
        } catch (SQLException ex) {
            Logger.getLogger(CustomerDataBase.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bean;
        
    }
        
}
